package ran.youling.service.impl.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import ran.youling.pojo.User;
import ran.youling.service.impl.UserService;
import ran.youling.vo.LoginUser;

import javax.annotation.Resource;
import java.util.Objects;

/***
 * NAME:Ran
 * TIME:2025/1/12
 */
@Service
public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {
    /**
     * 实现UserDetailsService重写loadUserByUsername方法 执行数据库操作
     *
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */


    @Resource
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", username);
        User user = userService.getOne(userQueryWrapper);
        if (Objects.isNull(user)) {
            throw new UsernameNotFoundException("暂无该用户");
        }
        return new LoginUser(user);
    }
}
